package com.cskaoyan.gateway.controller.order;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderDeleteService;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.*;
import com.mall.user.constants.SysRetCodeConstants;

import com.mall.user.intercepter.TokenInterceptor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;


import lombok.extern.slf4j.Slf4j;
import com.mall.order.OrderDeleteService;
import com.mall.user.constants.SysRetCodeConstants;


import lombok.extern.slf4j.Slf4j;

import com.mall.order.OrderDeleteService;
import com.mall.order.dto.DeleteOrderRequest;
import com.mall.order.dto.DeleteOrderResponse;
import com.mall.user.constants.SysRetCodeConstants;


import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import java.util.Map;


/**
 * @program: last-mall
 * @description:
 * @create: 2021-06-18 16:34
 **/
@Slf4j
@RestController
@RequestMapping("shopping")
@Api(tags = "OrderController", description = "订单控制层")
public class OrderController {

    @Reference
    OrderDeleteService orderDeleteService;

    @Reference
    OrderCoreService orderCoreService;

    @Reference
    OrderQueryService orderQueryService;


    /**
     * 创建订单
     */
    @PostMapping("order")
    @ApiOperation("创建订单")
    public ResponseData createOrder(@RequestBody CreateOrderRequest request, HttpServletRequest httpServletRequest) {
        String json = (String) httpServletRequest.getAttribute(TokenInterceptor.USER_INFO_KEY);
        JSONObject map = JSONUtil.parseObj(json); // 取出map
        long uid = Long.parseLong(map.get("uid").toString());

        request.setUserId(uid); // 将uid取出,得到用户id
        request.setUniqueKey(IdUtil.randomUUID()); // 生成UniqueKey

        CreateOrderResponse response = orderCoreService.createOrder(request);

        if (OrderRetCode.SUCCESS.getCode().equals(response.getCode())) {        // 返回结果
            return new ResponseUtil().setData(response.getOrderId());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @GetMapping("order")
    public ResponseData getOrderList(HttpServletRequest httpServletRequest, OrderListRequest request) {
        String json = (String) httpServletRequest.getAttribute(TokenInterceptor.USER_INFO_KEY);
        JSONObject map = JSONUtil.parseObj(json);
        long uid = Long.parseLong(map.get("uid").toString());//取出userId

        request.setUserId(uid);
        request.setSort("create_time");

        OrderListResponse response = orderQueryService.getOrderList(request);


        if (OrderRetCode.SUCCESS.getCode().equals(response.getCode())) {
            OrderListResponseResult result = new OrderListResponseResult();
            result.setData(response.getDetailInfoList());
            result.setTotal(response.getTotal());
            return new ResponseUtil().setData(result);

        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    @ApiOperation("删除订单")
    @ApiImplicitParam(name = "orderId", value = "订单id", dataType = "Integer", required = true)
    @DeleteMapping("order/{id}")
    public ResponseData deleteOrder(@PathVariable("id") String orderId) {
        DeleteOrderRequest deleteOrderRequest = new DeleteOrderRequest();
        deleteOrderRequest.setOrderId(orderId);
        DeleteOrderResponse response = orderDeleteService.deleteOrder(deleteOrderRequest);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(SysRetCodeConstants.SUCCESS.getMessage());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }


    //取消订单
    @PostMapping("cancelOrder")
    public ResponseData cancelOrder(@RequestBody CancelOrderRequest cancelOrderRequest) {

        CancelOrderResponse response = orderCoreService.cancelOrder(cancelOrderRequest);

        if (OrderRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response, "success");
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    //查询订单详情
    @GetMapping("order/{id}")
    public ResponseData getOrderDetail(@PathVariable("id") String orderId) {

        OrderDetailRequest orderDetailRequest = new OrderDetailRequest();
        orderDetailRequest.setOrderId(orderId);

        OrderDetailResponse response = orderQueryService.getOrderDetail(orderDetailRequest);


        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {

            OrderDetailDto orderDetailDto = response.getOrderDetailDto();
            return new ResponseUtil().setData(orderDetailDto);
        }

        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

}
